package org.ponly.lpf4j.impl;

import org.ponly.common.reflect.Reflect;
import org.ponly.lpf4j.Plugin;
import org.ponly.lpf4j.PluginDescriptor;
import org.ponly.lpf4j.PluginFactory;

/**
 */
public class DefaultPluginFactory implements PluginFactory {
    @Override
    public <P extends Plugin> P create(PluginDescriptor descriptor) {
        ClassLoader classLoader = descriptor.getPluginClassLoader();
        classLoader = null != classLoader ? classLoader : DefaultPluginManager.class.getClassLoader();
        String main = descriptor.getEntrance();

        // 可能只是依赖插件
        if (null != main) {
            try {
                Class<?> clazz = classLoader.loadClass(descriptor.getEntrance());
                return Reflect.from(clazz).create().get();
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
        return null;
    }
}
